<?php
/**
 * 菜单模块
 * User 1874
 */

namespace app\adminplatform\controller; 
use app\adminplatform\model\Menu as MenuModel; 
use app\adminplatform\model\AuthGroup; 
use think\facade\Request; 
use think\facade\View; 
use think\facade\Session; 

class Menu extends Initial{
    /*menu 列表*/
    public function menuList(){
        $userData = Session::get('userData');
        $map = [];

        $info = findChildLevel(MenuModel::getMenuList($map, 1)->toArray(),"me_pid","me_id");

        $rules = ['Menu/menuAdd', 'Menu/menuEdit', 'Menu/menuDel', 'Menu/menuDelAll'];
        $check = AuthGroup::checkMethodArray($userData, $rules);

        View::assign('info',$info);
        View::assign('check',$check);
        return View::fetch('menuList');
    }
    /*menu 新增*/
    public function menuAdd(){
        if(Request::isPost()){
            $map = Request::post();
            checkField($map);
            //插入数据
            $info = MenuModel::create($map);
            if($info){
                $data = ['info'=>'新增成功','status'=>1];
            }else{
                $data = ['info'=>'新增失败','status'=>0];
            }
            return json($data);
        }
        //是否接收到父id
        if(Request::get("pid")){
            //查询父级名称
            $parent = MenuModel::getMenuDetail(["me_id"=>Request::get("pid")])->me_name;
        }else{
            $parent = "顶级";
        }
        View::assign('parent',$parent);
        return View::fetch('menuAdd');
    }
    /*menu 编辑*/
    public function menuEdit(){
        if(Request::isPost()){
            $map = Request::post();
            checkField($map);
            //插入数据
            $info = MenuModel::update($map, ['me_id'=>$map["me_id"]]);
            if($info){
                $data = ['info'=>'编辑成功','status'=>1];
            }else{
                $data = ['info'=>'编辑无变化','status'=>0];
            }
            return json($data);
        }
        $id = Request::get('id');
        $info = MenuModel::getMenuDetail(["me_id"=>$id]);
        //父id > 0
        if($info["me_pid"] > 0){
            //查询父级名称
            $parent = MenuModel::getMenuDetail(["me_id"=>$info["me_pid"]])->me_name;
        }else{
            $parent = "顶级";
        }
        View::assign('parent',$parent);
        View::assign('info', $info);
        return View::fetch('menuEdit');
    }
    /*menu 删除*/
    public function menuDel(){
        if(Request::isPost()){
            $id = Request::post('id');
            //插入数据
            $info = MenuModel::where('me_id',$id)->delete();
            if($info){
                $data = ['info'=>'删除成功','status'=>1];
            }else{
                $data = ['info'=>'删除失败','status'=>0];
            }
        }else{
            $data = ['info'=>'请求错误','status'=>0];
        }
        return json($data);
    }
    /*menu 批量删除*/
    public function menuDelAll(){
        if(Request::isPost()){
            $ids = Request::post('ids');
            //删除数据
            $info = MenuModel::where('me_id', 'in', $ids)->delete();
            if($info){
                $data = ['info'=>'删除成功','status'=>1];
            }else{
                $data = ['info'=>'删除失败','status'=>0];
            }
        }else{
            $data = ['info'=>'请求错误','status'=>0];
        }
        return json($data);
    }
}